package com.gs.synedocket.service;

import static com.gs.synedocket.constant.AllConstants.TAG;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.Nullable;

import com.gs.synedocket.R;
import com.gs.synedocket.callback.IOnSyncListener;
import com.gs.synedocket.constant.AllConstants;
import com.gs.synedocket.constant.EventCode;
import com.gs.synedocket.constant.SyncStatus;
import com.gs.synedocket.db.bean.Event;
import com.gs.synedocket.util.NetworkUtil;
import com.gs.synedocket.util.SPUtil;
import com.gs.synedocket.util.SyncDownloadUtil;
import com.gs.synedocket.util.SyncUploadUtil;
import com.gs.synedocket.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;

public class AutoSyncDownloadService extends Service {

    private static boolean loop = true;
    private static Thread uploadThread;
    private IOnSyncListener onSyncListener;

    public static void stopThread() {
        loop = false;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "AutoSyncDownloadService: onStartCommand");
        loop = true;
        if (onSyncListener == null) {
            onSyncListener = this::sendBroadcast;
        }
        if (uploadThread == null) {
            uploadThread = new Thread(() -> {
                while (loop) {
                    try {
                        Thread.sleep(SPUtil.getInstance().getInt(AllConstants.SYNC_DOCKET_INTERVAL, getResources().getInteger(R.integer.download_interval)) * 1000L);
                    } catch (InterruptedException r) {

                    }
                    if (!SPUtil.getInstance().getBoolean(AllConstants.IS_AUTO, true)) continue;
                    if (SPUtil.getInstance().getString(AllConstants.TOKEN).isEmpty()) break;
                    if (!NetworkUtil.getInstance().isConnected()) continue;
                    if (SyncStatus.isSync()) continue;
//                    if (SyncUploadUtil.getInstance().isUpload()) continue;
//                    if (SyncDownloadUtil.getInstance().isDownload()) continue;
                    if (SPUtil.getInstance().getString(AllConstants.SYNC_TYPE, AllConstants.UPLOAD_DOCKET_INTERVAL).equals(AllConstants.UPLOAD_DOCKET_INTERVAL))
                        continue;
                    SyncDownloadUtil.getInstance().setOnSyncListener(onSyncListener);
                    SyncDownloadUtil.getInstance().download();
                }
            });
            uploadThread.start();
        }
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    private void sendBroadcast(int status, String msg) {
        EventBus.getDefault().post(new Event(EventCode.EVENT_AUTO_DOWNLOAD, status, msg));
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (uploadThread != null) {
            uploadThread = null;
        }
        Log.i(AllConstants.TAG, "AutoSyncDownload onDestroy");
    }
}
